#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/of_irq.h>
#include <linux/irq.h>
#include <linux/timer.h>

#define KEY_CNT			1		/* 设备号个数 	*/
#define KEY_NAME		"keyirq"	/* 名字 		*/

/* 定义按键值 */
#define KEY0VALUE		0X01	/* 按键值 		*/
#define INVAKEY			0XFF	/* 无效的按键值  */

struct irq_keydesc {
	int keyirq_gpio;						/* gpio */
	int irqnum;								/* 中断号     */
	unsigned char value;					/* 按键对应的键值 */
	char name[10];							/* 名字 */
	irqreturn_t (*handler)(int, void *);	/* 中断服务函数 */
};

/* keyirq设备结构体 */
struct keyirq_dev{
	dev_t devid;			/* 设备号 	 */
	struct cdev cdev;		/* cdev 	*/
	struct class *class;    /* 类 		*/
	struct device *device; 	/* 设备 	 */
	int major;				 /* 主设备号	  */
	int minor;				 /* 次设备号   */
	struct device_node	*nd; /* 设备节点 */
	atomic_t keyvalue;		  /* 按键值 		*/	
	struct timer_list timer;  /* 定时器 */
	struct irq_keydesc irq_keydesc;
	atomic_t releasekey;	  /* 标记是否完成一次完成的按键，包括按下和释放 */
	unsigned char curkeynum;  /* 当前的按键号 */
};

struct keyirq_dev keyirqdev;		/* keyirq设备 */

static irqreturn_t key0_handler(int irq, void *dev_id)
{
	struct keyirq_dev *dev = (struct keyirq_dev *)dev_id;

	dev->timer.data = (volatile long)dev_id;
	mod_timer(&dev->timer, jiffies + msecs_to_jiffies(10));	/* 10ms定时 */
	return IRQ_RETVAL(IRQ_HANDLED);
}

void timer_function(unsigned long arg)
{
	unsigned char value;

	struct irq_keydesc *keydesc;
	struct keyirq_dev *dev = (struct keyirq_dev *)arg;

	keydesc = &dev->irq_keydesc;

	value = gpio_get_value(keydesc->keyirq_gpio); 	/* 读取IO值 */
	if(value == 0){ 						/* 按下按键 */
		atomic_set(&dev->keyvalue, keydesc->value);
	}
	else{ 									/* 按键松开 */
		atomic_set(&dev->keyvalue, 0x80 | keydesc->value);
		atomic_set(&dev->releasekey, 1);	/* 标记松开按键，即完成一次完整的按键过程 */			
	}	
}


static int keyirqio_init(void)
{
	int ret = 0;
	keyirqdev.nd = of_find_node_by_path("/key");
	if (keyirqdev.nd== NULL) {
		return -EINVAL;
	}

	keyirqdev.irq_keydesc.keyirq_gpio = of_get_named_gpio(keyirqdev.nd ,"key-gpio", 0);
	if (keyirqdev.irq_keydesc.keyirq_gpio < 0) {
		printk("can't get keyirq0\r\n");
		return -EINVAL;
	}
	printk("keyirq_gpio=%d\r\n",keyirqdev.irq_keydesc.keyirq_gpio);
	
	/* 初始化keyirq所使用的IO */
	gpio_request(keyirqdev.irq_keydesc.keyirq_gpio, "keyirq0");	/* 请求IO */
	gpio_direction_input(keyirqdev.irq_keydesc.keyirq_gpio);	/* 设置为输入 */
	keyirqdev.irq_keydesc.irqnum = irq_of_parse_and_map(keyirqdev.nd, 0);
	printk("irqnum=%d\r\n",keyirqdev.irq_keydesc.irqnum);

	/* 申请中断 */
	keyirqdev.irq_keydesc.handler = key0_handler;
	keyirqdev.irq_keydesc.value = KEY0VALUE;
	ret = request_irq(keyirqdev.irq_keydesc.irqnum, keyirqdev.irq_keydesc.handler, 
		                 IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "KEYIRQ", &keyirqdev);
	if(ret < 0){
		printk("irq %d request failed!\r\n", keyirqdev.irq_keydesc.irqnum);
		return -EFAULT;
	}

	/* 定时器初始化 */
	init_timer(&keyirqdev.timer);
	keyirqdev.timer.function = timer_function;
	return 0;
}

static int keyirq_open(struct inode *inode, struct file *filp)
{
	filp->private_data = &keyirqdev; 	/* 设置私有数据 */
	return 0;
}

static ssize_t keyirq_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
	int ret = 0;
	unsigned char keyvalue = 0;
	unsigned char releasekey = 0;
	struct keyirq_dev *dev = (struct keyirq_dev *)filp->private_data;

	keyvalue = atomic_read(&dev->keyvalue);
	releasekey = atomic_read(&dev->releasekey);

	if (releasekey) { /* 有按键按下 */	
		if (keyvalue & 0x80) {
			keyvalue &= ~0x80;
			ret = copy_to_user(buf, &keyvalue, sizeof(keyvalue));
		} else {
			goto data_error;
		}
		atomic_set(&dev->releasekey, 0);/* 按下标志清零 */
	} else {
		goto data_error;
	}
	return 0;
	
data_error:
	return -EINVAL;
}

static int keyirq_release(struct inode *inode, struct file *filp)
{
	return 0;
}

/* 设备操作函数 */
static struct file_operations keyirq_fops = {
	.owner = THIS_MODULE,
	.open = keyirq_open,
	.read = keyirq_read,
	.release = 	keyirq_release,
};


static int __init mykeyirq_init(void)
{
	int ret = 0;
	/* 初始化原子变量 */
	atomic_set(&keyirqdev.keyvalue, INVAKEY);
	if (ret < 0) {
		return ret;
	}
	/* 注册字符设备驱动 */
	/* 1、创建设备号 */
	if (keyirqdev.major) {		/*  定义了设备号 */
		keyirqdev.devid = MKDEV(keyirqdev.major, 0);
		register_chrdev_region(keyirqdev.devid, KEY_CNT, KEY_NAME);
	} else {						/* 没有定义设备号 */
		alloc_chrdev_region(&keyirqdev.devid, 0, KEY_CNT, KEY_NAME);	/* 申请设备号 */
		keyirqdev.major = MAJOR(keyirqdev.devid);	/* 获取分配号的主设备号 */
		keyirqdev.minor = MINOR(keyirqdev.devid);	/* 获取分配号的次设备号 */
	}
	
	/* 2、初始化cdev */
	keyirqdev.cdev.owner = THIS_MODULE;
	cdev_init(&keyirqdev.cdev, &keyirq_fops);
	
	/* 3、添加一个cdev */
	cdev_add(&keyirqdev.cdev, keyirqdev.devid, KEY_CNT);

	/* 4、创建类 */
	keyirqdev.class = class_create(THIS_MODULE, KEY_NAME);
	if (IS_ERR(keyirqdev.class)) {
		return PTR_ERR(keyirqdev.class);
	}

	/* 5、创建设备 */
	keyirqdev.device = device_create(keyirqdev.class, NULL, keyirqdev.devid, NULL, KEY_NAME);
	if (IS_ERR(keyirqdev.device)) {
		return PTR_ERR(keyirqdev.device);
	}

	/* 6、始化按键 */
	atomic_set(&keyirqdev.keyvalue, INVAKEY);
	atomic_set(&keyirqdev.releasekey, 0);
	keyirqio_init();
	
	return 0;
}


static void __exit mykeyirq_exit(void)
{
	del_timer_sync(&keyirqdev.timer);	/* 删除定时器 */
	free_irq(keyirqdev.irq_keydesc.irqnum, &keyirqdev);
	/* 注销字符设备驱动 */
	cdev_del(&keyirqdev.cdev);/*  删除cdev */
	unregister_chrdev_region(keyirqdev.devid, KEY_CNT); /* 注销设备号 */

	device_destroy(keyirqdev.class, keyirqdev.devid);
	class_destroy(keyirqdev.class);
}

module_init(mykeyirq_init);
module_exit(mykeyirq_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zhangkewei");
